from flask import Blueprint, request
from flask_jwt_extended import create_access_token

from common import helper

system_api_bp = Blueprint("system_api", __name__)


@system_api_bp.route("/login", methods=["POST"])
def login():
    data = request.get_json()
    username = data.get("username")
    password = data.get("password")

    if helper.is_blank(username):
        return helper.validate_failure(msg="登录校验失败", errors={
            "username": "用户名不能为空"
        })

    if helper.is_blank(password):
        return helper.validate_failure(msg="登录校验失败", errors={
            "password": "密码不能为空"
        })

    access_token = create_access_token(identity=username)

    if username == "abc" and password == "123456":
        return helper.success(msg="登录成功", data={
            "token": access_token
        })
    else:
        return helper.validate_failure(msg="用户登录失败", errors={
            "password": "用户名或密码错误"
        })


@system_api_bp.route("/logout", methods=["POST"])
def logout():
    return helper.success(msg="登出成功")
